<template>
	<BaseContainer
		class="report-table-wrapper"
		v-loading="loading"
		element-loading-text="加载中..."
		element-loading-background="rgba(255, 255, 255, 0.8)"
	>
		<div class="r-header">
			<p>养老机构消防改造补贴申请表</p>
		</div>
		<div class="flex gap-20">
			<div style="min-width: 140px">填报单位（盖章）：{{ modelData.organizationName }}</div>
			<div>填报时间：{{ modelData.fillingTime }}</div>
		</div>
		<ReportTable
			:column="colConfigList"
			:isDetail="isDetail"
			:moreNewsModel="moreNewsModel"
			:baseModel="baseModel"
			:model="modelData"
			@change="handleSave"
		>
			<template v-slot:subsidy_big_money>
				<span>大写：</span>
				<span>{{ subsidy_big_money }}</span>
			</template>
			<template v-slot:note>
				<div class="w-100 tr flex-column">
					<p class="fc">本机构承诺以上及所附数据资料真实有效，如有不实，愿承担相关处罚。</p>
					<div class="p-r-50">
						<p>承办人签名： （单位盖章）</p>
						<p style="padding-right: 3vw">年 月 日</p>
					</div>
				</div>
			</template>
			<template v-slot:opinion>
				<div class="w-100" style="line-height: 30px">
					<p style="padding-right: 2vw; text-align: right; letter-spacing: 4px">区民政局：（章）</p>
					<p style="padding-right: 3vw; text-align: right; letter-spacing: 10px">年 月 日</p>
				</div>
			</template>
		</ReportTable>
		<OrgArchives ref="orgArchivesEl"></OrgArchives>
	</BaseContainer>
</template>

<script setup lang="ts">
import OrgArchives from '../OrgArchives/index.vue'
import { onUnmounted } from 'vue'
import ReportTable from '../ReportTable/index.vue'

import apiMod from '@/apiMod'
import { useCommonStore } from '@/stores/common'
import { dealBigMoney, filterEmptyObj } from '@/utils/util'
import { computed } from 'vue'
const { setTempParams, setGloablParams, setMoreSearch } = useCommonStore()

const props = withDefaults(
	defineProps<{
		creditCode?: string | number
		params?: any
		editData?: any
		isDetail?: boolean
		caseId?: string | number
		custome?: boolean
		approvalStatus?: string | number
	}>(),
	{
		editData: {},
	}
)
const emit = defineEmits(['save'])
const orgArchivesEl = ref(null)
const loading = ref(false)
const model = ref({
	year: '',
	month: '',
})
const baseModel: any = shallowRef({})
let moreNewsModel: any = ref({
})
let modelData: any = ref({
})


const subsidy_big_money = computed(() => {
	const money = modelData.value.subsidyMoney
	if (money && !isNaN(Number(money))) {
		return dealBigMoney(+money)
	} else {
		return ''
	}
})
const onClose = () => {
	setTempParams({})
	setGloablParams({})
	setMoreSearch({})
}

const tableColumnConfig = (data?: any) => {
	const config = [
		[
			{
				m: '养老机构基本情况',
				bl: 1,
				fs: 16,
				mc: {
					rs: 1,
					cs: 4,
				},
			},
		],
		[
			{ m: '机构名称', style: { width: '200px' } },
			{
				m: '',
				prop: 'organizationName',
				linkType: true,
				setColor: true,
				archives: true,
				archivesText: '档案',
				showLink: (col, model) => {
					orgArchivesEl.value?.getServiceOrgInfo(model.creditCode)
				},
			},
			{ m: '法定代表人', style: { width: '200px' } },
			{ m: '', prop: 'legalUserName', setColor: true },
		],
		[{ m: '机构地址' }, { m: '', setColor: true, prop: 'organizationAddress' }, { m: '邮政编码' }, { m: '', setColor: true, prop: 'postalCode' }],
		[{ m: '联系人' }, { m: '', setColor: true, prop: 'contactName' }, { m: '联系电话' }, { m: '', setColor: true, prop: 'contactPhone' }],
		[{ m: '开户银行' }, { setColor: true, m: '', prop: 'bankName' }, { m: '银行账号' }, { setColor: true, m: '', prop: 'bankAccount' }],
		[
			{
				m: '申请基本情况',
				bl: 1,
				fs: 16,
				style: { borderTop: '0px solid #fff' },
				mc: {
					rs: 1,
					cs: 4,
				},
			},
		],
		[
			{
				m: '审计改造金额',
				mc: {
					rs: 1,
					cs: 1,
				},
			},
			{
				m: '',
				prop: 'auditMoney',
				mc: {
					rs: 1,
					cs: 1,
				},
			},
			{
				m: '补贴标准',
				mc: {
					rs: 1,
					cs: 1,
				},
			},
			{
				m: '',
				prop: 'subsidyStandards',
				mc: {
					rs: 1,
					cs: 1,
				},
			},
		],
		[
			{
				m: '补贴金额',
				align: 'center',
				mc: {
					rs: 1,
					cs: 1,
				},
			},
			{
				m: '',
				prop: 'subsidyMoney',
				align: 'left',
				mc: {
					rs: 1,
					cs: 2,
				},
			},
			{
				propName: '大写',
				prop: 'subsidy_big_money',
				disabled: true,
				align: 'left',
				mc: {
					rs: 1,
					cs: 2,
				},
			},
		],
		[
			{
				prop: 'note',
				mc: {
					rs: 1,
					cs: 9,
				},
			},
		],
		[
			{
				m: '区民政部门意见',
				mc: {
					rs: 1,
					cs: 2,
				},
			},
			{
				m: '',
				prop: 'opinion',
				mc: {
					rs: 1,
					cs: 7,
				},
			},
		],
	]
	return config
}
const colConfigList = ref(tableColumnConfig())

const getInitData = async () => {
	const params = props.params || {}
	const query = {
		year: '',
		month: '',
		creditCode: props.creditCode,
		projectId: props.params.subsidyProjectId,
	}
	if (params.belongDate) {
		query.year = params.belongDate.replace('年', '-').replace('月', '').split('-')[0]
		query.month = params.belongDate.replace('年', '-').replace('月', '').split('-')[1]
		model.value.year = query.year
		model.value.month = query.month
	} else {
		return
	}
	modelData.value = props.editData
	try {
		const condition = (props.caseId && props.isDetail) || props.custome || props.approvalStatus == -2
		loading.value = true
    let resultData = {}
    if(condition) {
      resultData = props.editData
    }else {
		  const uptoData = await apiMod.view_metadata_statistics_fire_ls(query).then((res) => res.data)
      resultData = uptoData
    }
		baseModel.value = JSON.parse(JSON.stringify(resultData))
		const reportData = filterEmptyObj(resultData)
		let mixData = {
			...props.editData,
			...reportData,
		}
		colConfigList.value = tableColumnConfig()
		loading.value = false
		modelData.value = mixData
		if (!condition) handleSave()
	} catch (error) {
		loading.value = false
	}
}

watch(
	() => props.editData?.id,
	(val) => {
		if (val) {
			if (props.params) getInitData()
		}
	},
	{
		immediate: true,
	}
)

onMounted(() => {
	nextTick(() => {
		if (props.params) getInitData()
	})
})
const handleSave = () => {
  modelData.value.capitalLetters = subsidy_big_money.value
  // if(!modelData.value.fillingTime) modelData.value.fillingTime =new Date().toLocaleString().split(' ')[0]

	emit('save', modelData.value, 'fire_control_report_1001', props.editData)
}
</script>

<style scoped lang="scss">
.flex-column {
	display: flex;
	flex-flow: column;
	justify-content: space-around;
	height: 100px;
}
.text-underline {
	text-decoration: underline;
	padding: 0 6px;
}
.report-table-wrapper {
	width: 100%;
	margin: 0 auto;
	padding: 10px 30px;
	top: 0;
	.r-header {
		text-align: center;
		font-size: 18px;
		font-weight: bold;
		padding: 15px 0;

		.text-sm {
			font-weight: 400;
			font-size: 16px;
		}
	}
}
</style>
